package com.ColonyCount.Activity;

import com.ColonyCount.ColonyCountContext;
import com.ColonyCount.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Debug;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MenuActivity extends Activity {
	
	private static final int SELECT_PICTURE = 1;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menu);
		
		Button buttonOpenFile = (Button) findViewById(R.id.buttonOpenFile);
		buttonOpenFile.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				startFileBrowserActivity(MenuActivity.this);
			}
		});

		Button buttonCamera = (Button) findViewById(R.id.buttonCamera);
		buttonCamera.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
			}
		});

//		Button buttonOptions = (Button) findViewById(R.id.buttonOptions);
//		buttonOptions.setOnClickListener(new OnClickListener() {
//			public void onClick(View v) {
//				startOptionsActivity(MenuActivity.this);
//			}
//		});

	}
	
	@Override
	protected void onResume() {
		super.onResume();
		ColonyCountContext.dumpActivityEvent(this, "MenuActivity.onResume()");
	}
	
    @Override
	protected void onPause() {
		super.onPause();
		ColonyCountContext.dumpActivityEvent(this, "MenuActivity.onPause()");
	}
	
	void startROIActivity(Context context)
	{
		Intent myIntent = new Intent(context, ROIActivity.class);
        startActivity(myIntent);
	}

//	void startOptionsActivity(Context context)
//	{
//		Intent myIntent = new Intent(context, PreferencesActivity.class);
//        startActivity(myIntent);
//	}

	void startFileBrowserActivity(Context context)
	{
		 Intent intent = new Intent();
         intent.setType("image/*");
         intent.setAction(Intent.ACTION_GET_CONTENT);
         startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
	}

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();

                //MEDIA GALLERY
            	String selectedImagePath = getPath(selectedImageUri);
                if(selectedImagePath == null)
                {
                	selectedImagePath = selectedImageUri.getPath();
                }
                
                ColonyCountContext cccontext = ColonyCountContext.getSingleton();
                cccontext.setImageFileName(selectedImagePath);
                
                
                // TEST
                cccontext.createSourceBitmap(this);
                cccontext.createLuma();
                
        		//startROIActivity(getApplicationContext());
                
                Intent myIntent = new Intent(getApplicationContext(), ThresholdActivity.class);
                startActivity(myIntent);
            }
        }
    }

    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if(cursor!=null)
        {
            //HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
            //THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
            int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
        else return null;
    }
}
